package com.dkd.manager.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.dkd.common.core.domain.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manager.domain.TaskDetails;
import com.dkd.manager.service.ITaskDetailsService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;

/**
 * 工单详情，只有补货工单才有Controller
 * 
 * @author wanghuan
 * @date 2025-01-06
 */
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/manager/taskDetails")
@Api(tags = "工单详情管理") // 描述控制器的功能
public class TaskDetailsController extends BaseController {

    @Autowired
    private ITaskDetailsService taskDetailsService;

    /**
     * 查询工单详情，只有补货工单才有列表
     */
    @PreAuthorize("@ss.hasPermi('manager:details:list')")
    @ApiOperation(value = "获取工单详情列表", notes = "根据查询条件获取所有符合条件的补货工单详情")
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam(value = "工单详情对象，用于包装查询参数", required = false) TaskDetails taskDetails) {
        startPage();
        List<TaskDetails> list = taskDetailsService.selectTaskDetailsList(taskDetails);
        return getDataTable(list);
    }

    /**
     * 导出工单详情，只有补货工单才有列表
     */
    @PreAuthorize("@ss.hasPermi('manager:details:export')")
    @Log(title = "工单详情，只有补货工单才有", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出工单详情列表为Excel文件", notes = "将所有符合条件的补货工单详情导出为Excel文件")
    @PostMapping("/export")
    public void export(HttpServletResponse response,
                       @ApiParam(value = "工单详情对象，用于包装查询参数", required = false) TaskDetails taskDetails) {
        List<TaskDetails> list = taskDetailsService.selectTaskDetailsList(taskDetails);
        ExcelUtil<TaskDetails> util = new ExcelUtil<TaskDetails>(TaskDetails.class);
        util.exportExcel(response, list, "工单详情，只有补货工单才有数据");
    }

    /**
     * 获取工单详情，只有补货工单才有详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:details:query')")
    @ApiOperation(value = "通过ID获取工单详情", notes = "根据ID查询具体的补货工单详情")
    @GetMapping(value = "/{detailsId}")
    public AjaxResult getInfo(@PathVariable("detailsId")
                              @ApiParam(value = "工单详情ID", required = true) Long detailsId) {
        return success(taskDetailsService.selectTaskDetailsByDetailsId(detailsId));
    }

    /**
     * 新增工单详情，只有补货工单才有
     */
    @PreAuthorize("@ss.hasPermi('manager:details:add')")
    @Log(title = "工单详情，只有补货工单才有", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增一个工单详情", notes = "保存一个新的补货工单详情到数据库")
    @PostMapping
    public AjaxResult add(@RequestBody
                          @ApiParam(value = "工单详情对象", required = true) TaskDetails taskDetails) {
        return toAjax(taskDetailsService.insertTaskDetails(taskDetails));
    }

    /**
     * 修改工单详情，只有补货工单才有
     */
    @PreAuthorize("@ss.hasPermi('manager:details:edit')")
    @Log(title = "工单详情，只有补货工单才有", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更新工单详情", notes = "根据ID更新指定的补货工单详情")
    @PutMapping
    public AjaxResult edit(@RequestBody
                           @ApiParam(value = "工单详情对象", required = true) TaskDetails taskDetails) {
        return toAjax(taskDetailsService.updateTaskDetails(taskDetails));
    }

    /**
     * 删除工单详情，只有补货工单才有
     */
    @PreAuthorize("@ss.hasPermi('manager:details:remove')")
    @Log(title = "工单详情，只有补货工单才有", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除工单详情", notes = "根据ID数组批量删除补货工单详情")
    @DeleteMapping("/{detailsIds}")
    public AjaxResult remove(@PathVariable
                             @ApiParam(value = "要删除的工单详情ID数组", required = true) Long[] detailsIds) {
        return toAjax(taskDetailsService.deleteTaskDetailsByDetailsIds(detailsIds));
    }

    /**
     * 查看工单补货详情
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:list')")
    @ApiOperation(value = "根据工单ID查看工单补货详情", notes = "根据任务ID查询相关的补货工单详情")
    @GetMapping(value = "/byTaskId/{taskId}")
    public R<List<TaskDetails>> byTaskId(@PathVariable("taskId")
                               @ApiParam(value = "工单ID", required = true) Long taskId) {
        TaskDetails taskDetailsParam = new TaskDetails();
        taskDetailsParam.setTaskId(taskId);
        return R.ok(taskDetailsService.selectTaskDetailsList(taskDetailsParam));
    }
}
